package flc.ast.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.stark.calculator.databinding.FragmentTaxSelCityBinding;
import com.stark.calculator.tax.SelectCityFragment;
import com.stark.calculator.tax.adapter.PickItemAdapter;
import com.stark.calculator.tax.model.CityWage;
import com.stark.calculator.tax.model.DataProvider;
import com.stark.calculator.tax.viewmodel.SelectCityViewModel;

import java.util.Arrays;

import flc.ast.R;
import flc.ast.adapter.MyPickItemAdapter;
import stark.common.basic.base.BaseFragment;
import stark.common.basic.event.EventStatProxy;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link MySelectCityFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class MySelectCityFragment extends BaseFragment<SelectCityViewModel, FragmentTaxSelCityBinding> {

    private SelectCityFragment.IListener mListener;
    private MyPickItemAdapter mAdapter;

    @Override
    protected int onCreate() {
        return R.layout.fragment_tax_sel_city;
    }

    @Override
    protected void initView() {
        //event
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.rlEv1Container);

        //recycleView
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        mDataBinding.rvCity.setLayoutManager(layoutManager);
        MyPickItemAdapter adapter = new MyPickItemAdapter();
        mAdapter = adapter;
        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                CityWage cityWage = mViewModel.getCityWageList().get(position);
                if (mListener != null) {
                    mListener.onSelectCity(cityWage);
                }
            }
        });
        mDataBinding.rvCity.setAdapter(adapter);

        //indexBar
        mDataBinding.indexBar.setLetters(Arrays.asList(DataProvider.LETTERS));
        mDataBinding.indexBar.setOnLetterChangeListener((position, letter) -> {
            layoutManager.scrollToPositionWithOffset(adapter.getFirstPosBy(letter), 0);
        });
    }

    @Override
    protected void initData() {
    }

    @Override
    protected SelectCityViewModel initViewModel() {
        return new ViewModelProvider(this).get(SelectCityViewModel.class);
    }

    @Override
    protected void initObserve() {
        super.initObserve();
        mViewModel.getPickItemList().observe(this, pickItems -> {
            if (mAdapter != null) mAdapter.setNewInstance(pickItems);
        });
    }

    @Override
    protected void showError(Object obj) {
    }

    public void setListener(SelectCityFragment.IListener listener) {
        this.mListener = listener;
    }

    public interface IListener {

        void onSelectCity(CityWage cityWage);

    }

}